package com.example.professorevaluation

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Telephony
import android.view.MenuItem
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.professorevaluation.adapter.EvaluationAdapter
import com.example.professorevaluation.bean.ProfessorBean
import com.example.professorevaluation.bean.RateBean
import com.example.professorevaluation.dao.ProfessorDatabase
import com.example.professorevaluation.databinding.ActivityProfessorDetailBinding

class ProfessorDetailActivity : AppCompatActivity() {
    private lateinit var binding: ActivityProfessorDetailBinding
    private var mName: String? = null
    private var mCourse: String? = null
    private var mUniversity: String? = null
    private var difficulty: Int = 0
    private var rate: Int = 0
    private lateinit var mAdapter: EvaluationAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityProfessorDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        mName = intent.getStringExtra("name")
        mCourse = intent.getStringExtra("course")
        mUniversity = intent.getStringExtra("university")
        setSupportActionBar(binding.toolbar)
        val actionBar = supportActionBar
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true)
            actionBar.title = "Professor Information"
        }
        binding.tvName.text = mName
        binding.tvCourse.text = mCourse
        binding.tvUniversity.text = mUniversity
        val list: MutableList<RateBean> = ArrayList()
        val newList: MutableList<RateBean> = ArrayList()
        list.addAll(ProfessorDatabase.getInstance(this).evaluationDao().getAll())
        for (newBean in list){
            if (newBean.name == mName){
                newList.add(newBean)
            }
        }
        if (newList.size == 0){
            binding.tvDifficulty.text = "--"
            binding.tvPoint.text = "--"
            binding.tvTip.visibility = View.VISIBLE
        }else{
            for (bean in newList){
                if (mName == bean.name){
                    difficulty += bean.difficulty
                    rate += bean.rate
                }
            }
            binding.tvDifficulty.text = (difficulty / newList.size).toString()
            binding.tvPoint.text = (rate / newList.size).toString()
        }
        mAdapter = EvaluationAdapter(newList, this)
        binding.recyclerView.apply {
            layoutManager = LinearLayoutManager(this@ProfessorDetailActivity)
            adapter = mAdapter
        }
        mAdapter.notifyDataSetChanged()
        binding.tvRate.setOnClickListener {
            startActivityForResult(Intent(this, RateActivity::class.java).putExtra("name", mName), 1000)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1000 && resultCode == RESULT_OK){
            difficulty = 0
            rate = 0
            val list: MutableList<RateBean> = ArrayList()
            val newList: MutableList<RateBean> = ArrayList()
            list.addAll(ProfessorDatabase.getInstance(this).evaluationDao().getAll())
            for (newBean in list){
                if (newBean.name == mName){
                    newList.add(newBean)
                }
            }
            if (newList.size == 0){
                binding.tvDifficulty.text = "--"
                binding.tvPoint.text = "--"
                binding.tvTip.visibility = View.VISIBLE
            }else{
                mAdapter.update(newList)

                for (bean in newList){
                    difficulty += bean.difficulty
                    rate += bean.rate
                }
                binding.tvTip.visibility = View.GONE
                binding.tvDifficulty.text = (difficulty / newList.size).toString()
                binding.tvPoint.text = (rate / newList.size).toString()
            }
        }
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item)
    }
}